document.addEventListener('DOMContentLoaded', function () {
    // 导航栏滚动效果
    const floatingElements = document.querySelector('.floating-elements');
    const elements = document.querySelectorAll('.element');

    elements.forEach((element, index) => {
        // 随机大小
        const size = Math.random() * 20 + 10;
        element.style.width = `${size}px`;
        element.style.height = `${size}px`;

        // 随机位置
        const x = Math.random() * 100;
        const y = Math.random() * 100;
        element.style.left = `${x}%`;
        element.style.top = `${y}%`;

        // 随机动画延迟
        const delay = Math.random() * 5;
        element.style.animationDelay = `${delay}s`;

        // 随机动画持续时间
        const duration = Math.random() * 10 + 10;
        element.style.animationDuration = `${duration}s`;
    });

    // 鼠标移动效果
    document.addEventListener('mousemove', (e) => {
        const glowElements = document.querySelectorAll('.glow');
        const x = e.clientX / window.innerWidth;
        const y = e.clientY / window.innerHeight;

        glowElements.forEach((glow, index) => {
            const moveX = (x - 0.5) * 50;
            const moveY = (y - 0.5) * 50;
            glow.style.transform = `translate(${moveX}px, ${moveY}px)`;
        });
    });

    // 背景网格动画
    const particles = document.querySelector('.particles');
    let angle = 0;

    function updateGrid() {
        angle += 0.5;
        particles.style.background = `
            radial-gradient(circle at ${50 + Math.sin(angle) * 20}% ${50 + Math.cos(angle) * 20}%, 
            rgba(0, 255, 157, 0.1) 0%, transparent 20%),
            radial-gradient(circle at ${50 + Math.cos(angle) * 20}% ${50 + Math.sin(angle) * 20}%, 
            rgba(0, 255, 157, 0.1) 0%, transparent 20%)
        `;
        requestAnimationFrame(updateGrid);
    }
    updateGrid();
    
    const header = document.querySelector('.header');
    let lastScroll = 0;

    window.addEventListener('scroll', () => {
        const currentScroll = window.pageYOffset;

        if (currentScroll <= 0) {
            header.classList.remove('scroll-up');
            return;
        }

        if (currentScroll > lastScroll && !header.classList.contains('scroll-down')) {
            // 向下滚动
            header.classList.remove('scroll-up');
            header.classList.add('scroll-down');
        } else if (currentScroll < lastScroll && header.classList.contains('scroll-down')) {
            // 向上滚动
            header.classList.remove('scroll-down');
            header.classList.add('scroll-up');
        }
        lastScroll = currentScroll;
    });

    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();
            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                target.scrollIntoView({
                    behavior: 'smooth',
                    block: 'start'
                });
            }
        });
    });

    // 游戏卡片动画
    const categoryCards = document.querySelectorAll('.category-card');

    const observerOptions = {
        threshold: 0.1
    };

    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('animate');
            }
        });
    }, observerOptions);

    categoryCards.forEach(card => {
        observer.observe(card);
    });

    // 热门游戏轮播
    const featuredGames = [
        {
            title: "暗影之刃",
            genre: "动作冒险",
            rating: "9.5分",
            image: "https://via.placeholder.com/300x400"
        },
        {
            title: "星际指挥官",
            genre: "策略游戏",
            rating: "9.2分",
            image: "https://via.placeholder.com/300x400"
        },
        {
            title: "午夜惊魂",
            genre: "恐怖惊悚",
            rating: "8.9分",
            image: "https://via.placeholder.com/300x400"
        }
    ];

    let currentGame = 0;
    const gameSlider = document.querySelector('.game-slider');

    function updateGamePreview() {
        const game = featuredGames[currentGame];
        gameSlider.innerHTML = `
            <div class="game-preview">
                <img src="${game.image}" alt="${game.title}">
                <div class="game-info">
                    <h3>${game.title}</h3>
                    <p>${game.genre} | ${game.rating}</p>
                </div>
            </div>
        `;
    }

    // 每5秒切换一次热门游戏
    setInterval(() => {
        currentGame = (currentGame + 1) % featuredGames.length;
        updateGamePreview();
    }, 5000);

    // 游戏链接点击效果
    const gameLinks = document.querySelectorAll('.game-link');
    gameLinks.forEach(link => {
        link.addEventListener('click', function (e) {
            e.preventDefault();
            this.classList.add('clicked');
            setTimeout(() => {
                window.location.href = this.getAttribute('href');
            }, 300);
        });
    });

    // 添加页面加载动画
    document.body.classList.add('loaded');
}); 